package com.example.wmstore.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wmstore.common.QueryPageParam;
import com.example.wmstore.common.Result;
import com.example.wmstore.entity.*;
import com.example.wmstore.service.GoodsService;
import com.example.wmstore.service.InrecordService;
import com.example.wmstore.service.OutrecordService;
import com.example.wmstore.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author TCW
 * @since 2024-05-29
 */
@RestController
@RequestMapping("/storage")
public class StorageController {
    @Autowired
    private StorageService storageService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private InrecordService inrecordService;
    @Autowired
    private OutrecordService outrecordService;

    @GetMapping("/list")
    public List<Storage> list(){
        return storageService.list();
    }
    @GetMapping("/count")
    public Integer count(){
        return storageService.list().size();
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Storage storage){
        List<Storage> list=storageService.lambdaQuery().eq(Storage::getName,storage.getName()).list();
        if(list.size()>0){
            return Result.fail("名称重复，创建失败");
        }
        return storageService.save(storage)?Result.suc(storage):Result.fail("创建失败");
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Storage storage){
        List<Storage> list=storageService.lambdaQuery().eq(Storage::getName,storage.getName()).
                ne(Storage::getId,storage.getId()).list();
        if(list.size()>0){
            return Result.fail("名称重复，创建失败");
        }
        return storageService.updateById(storage)?Result.suc(storage):Result.fail();
    }
    //删除
    @GetMapping("/delete")
    public Result del(@RequestParam Integer id){
        List<Goods> list =goodsService.lambdaQuery().eq(Goods::getStorage,id).list();
        for(Goods goods:list)
        {
            inrecordService.lambdaQuery().eq(Inrecord::getGoods,goods.getId()).list().forEach(inrecordService::removeById);
            outrecordService.lambdaQuery().eq(Outrecord::getGoods,goods.getId()).list().forEach(outrecordService::removeById);
        }
        goodsService.lambdaQuery().eq(Goods::getStorage,id).list().forEach(goodsService::removeById);
        return storageService.removeById(id)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String status=(String)param.get("status");
        Page<Storage> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Storage> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Storage::getName,name);
        }

        if(StringUtils.isNotBlank(status) && !"null".equals(status)){
            lambdaQueryWrapper.eq(Storage::getStatus,status);
        }

        IPage result = storageService.PageList(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        Storage storage=storageService.getById(id);
        return Result.suc(storage);
    }

}
